package gui;

import domein.WebinarController;
import java.awt.event.KeyEvent;
import java.util.Observable;
import java.util.Observer;

/**
 *
 * @author Robin
 */
public class LoginFrame extends javax.swing.JFrame implements Observer {

    private WebinarController wc;

    /** Creates new form Login */
    public LoginFrame() {
        super("Inloggen");
        wc = WebinarController.getInstance();

        initComponents();
    }
    String error = "";

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_paswoord = new javax.swing.JPasswordField();
        txt_gebruikersnaam = new javax.swing.JTextField();
        Button_Inloggen = new javax.swing.JButton();
        Button_Annuleer = new javax.swing.JButton();
        Button_Nieuw = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        lblErr = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setResizable(false);

        jLabel1.setText("Gebruikersnaam:");
        jLabel1.setToolTipText("Gebruikernaam is uw voornaam en familienaam aan elkaar.");

        jLabel2.setText("Wachtwoord:");

        txt_paswoord.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_paswoordKeyPressed(evt);
            }
        });

        txt_gebruikersnaam.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_gebruikersnaamKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txt_gebruikersnaam)
                    .addComponent(txt_paswoord, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_gebruikersnaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_paswoord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        Button_Inloggen.setText("Inloggen");
        Button_Inloggen.setName("Button_Inloggen"); // NOI18N
        Button_Inloggen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button_InloggenActionPerformed(evt);
            }
        });

        Button_Annuleer.setText("Annuleer");
        Button_Annuleer.setName("Button_Annuleer"); // NOI18N
        Button_Annuleer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button_AnnuleerActionPerformed(evt);
            }
        });

        Button_Nieuw.setText("Nieuwe Gebruiker");
        Button_Nieuw.setName("Button_Nieuw"); // NOI18N
        Button_Nieuw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button_NieuwActionPerformed(evt);
            }
        });

        lblErr.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        lblErr.setForeground(new java.awt.Color(204, 51, 0));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(lblErr, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblErr, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(6, 6, 6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Button_Nieuw)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                        .addComponent(Button_Annuleer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Button_Inloggen))))
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(6, 6, 6))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Button_Inloggen)
                    .addComponent(Button_Annuleer)
                    .addComponent(Button_Nieuw))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Button_InloggenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button_InloggenActionPerformed
        inloggen();
    }//GEN-LAST:event_Button_InloggenActionPerformed

    private void Button_NieuwActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button_NieuwActionPerformed
        new NieuweGebruiker().setVisible(true);


    }//GEN-LAST:event_Button_NieuwActionPerformed

    private void Button_AnnuleerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button_AnnuleerActionPerformed
        // TODO add your handling code here:
        setVisible(false);
        dispose();
        System.exit(EXIT_ON_CLOSE);
    }//GEN-LAST:event_Button_AnnuleerActionPerformed

    private void txt_gebruikersnaamKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_gebruikersnaamKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            inloggen();
        }
    }//GEN-LAST:event_txt_gebruikersnaamKeyPressed

    private void txt_paswoordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_paswoordKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            inloggen();
        }
    }//GEN-LAST:event_txt_paswoordKeyPressed

    /**
     * @param args the command line arguments
     */
    private void inloggen() {
        error = "";
        lblErr.setText(error);

        if (txt_gebruikersnaam.getText().trim().equals("")) {
            error = "Geen gebruikersnaam ingevuld";
            lblErr.setText(error);
        } else if (!wc.gebruikersnaamControleren(txt_gebruikersnaam.getText().toLowerCase())) {
            error = "Gebruikersnaam bestaat niet.";
            lblErr.setText(error);
        } else if (txt_paswoord.getText().trim().equals("")) {
            error = "Geen wachtwoord ingegeven.";
            lblErr.setText(error);
        } else if (!wc.wachtwoordControleren(txt_gebruikersnaam.getText(), txt_paswoord.getText())) {
            error = "Wachtwoord niet correct.";
            lblErr.setText(error);
        } else {
            if (wc.gebruikersnaamControleren(txt_gebruikersnaam.getText().toLowerCase())
                    && wc.wachtwoordControleren(txt_gebruikersnaam.getText(), txt_paswoord.getText())) {
                new WebinarMain(wc.getGebruikerID(txt_gebruikersnaam.getText())).setVisible(true);

                dispose();
            }
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Button_Annuleer;
    private javax.swing.JButton Button_Inloggen;
    private javax.swing.JButton Button_Nieuw;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblErr;
    private javax.swing.JTextField txt_gebruikersnaam;
    private javax.swing.JPasswordField txt_paswoord;
    // End of variables declaration//GEN-END:variables

    //implementatie observer pattern
    @Override
    public void update(Observable arg0, Object arg1) {
        if (arg1.equals("nieuweGebruiker")) {
            wc.updateGebruikers();
        }

    }
    //einde observer
}
